<?php
namespace Amoysea\Controller;
use Think\Controller;
class IdcardController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        $g=session('company');
		C('erp_dbname',$g['db']);
        $db_name=$g['db'];
    }
    public function index(){
        $m=M('Idcard', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));

		$param=I('param.','','trim');
		$name=$param['name'];
		$no=$param['no'];		
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($name)){
			$map['name'] = array('like','%'.$name.'%');
        }
		if(!empty($no)){
			$map['no'] = array('like','%'.$no.'%');
        }
		//时间查询-begin
		$start_time=$param['start_time'];//开始时间
		$end_time=$param['end_time'];//结束时间
		if(!empty($start_time) && empty($end_time)) {
			$start_time=strtotime($start_time);
			$map['_string']="add_time > '$start_time' ";
        }
        if(empty($start_time) && !empty($end_time)) {
			$end_time=strtotime($end_time)+3600*24;
			$map['_string']="add_time < '$end_time' ";
        }
		if(!empty($start_time) && !empty($end_time)) {
			$start_time=strtotime($start_time);
			$end_time=strtotime($end_time)+3600*24;
			$map['_string']="add_time > '$start_time' and add_time < '$end_time'";
        }
		// dump($param);
		//时间查询-end
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		// echo $m->getlastsql();

		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
	public function add(){
		$province_list=province_list();
		$this->assign('province_list',$province_list);
		$this->display();
	}
	public function city_list(){
		$code=I('code');
		$city_list=city_list($code);
		alert_json(1,$city_list);
	}
	public function area_list(){
		$code=I('code');
		$province_name=I('province_name');
		// dump(I());
		$area_list=area_list($province_name,$code);
		alert_json(1,$area_list);
	}
	//添加
	public function confirm_add(){
			$m=M('Idcard', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			if (!empty($_FILES)){
				$config = array(
					'maxSize'    =>    3145728,    
					'savePath'   =>    '/Import/',    
					'saveName'   =>    array('uniqid',''),    
					'exts'       =>    array('jpg','jpeg','png','gif'),    
					'autoSub'    =>    true,    
					'subName'    =>    array('date','Ymd'),);
				$upload = new \Think\Upload($config);// 实例化上传类
				$info   =   $upload->upload();    
				if(!$info) {// 上传错误提示错误信息        
					$this->error($upload->getError());    
				}
				$extensionf=$info['pic_f']['ext'];
				$file_namef='./Uploads'.$info['pic_f']['savepath'].$info['pic_f']['savename'];
				
				textgd($file_namef);//加水印
				$param['pic_f']=$file_namef;
				
				$extensionb=$info['pic_b']['ext'];
				$file_nameb='./Uploads'.$info['pic_b']['savepath'].$info['pic_b']['savename'];
				
				textgd($file_nameb);//加水印
				$param['pic_b']=$file_nameb;
			}
			$this->verification($param);
			$this->files();
			$dup=$m->where(array('no'=>$param['no']))->find();
			
			if($dup){
				$this->error('身份证号已经存在');
			}
			$param['add_time']=time();
			// dump($param);
			$res=$m->add($param);
			if($res){
				$this->success('添加成功');
			}
	}
	public function verification($param){
			// dump($param);
			if(empty($param['name'])){
				$this->error('姓名不能为空');
			}
			if(empty($param['tel'])){
				$this->error('联系方式不能为空');
			}
			if(!mobile($param['tel']) && !isTel($param['tel'])){
				$this->error('联系方式(手机号)错误');
			}
			if(empty($param['address'])){
				$this->error('地址不能为空');
			}
			if(empty($param['no'])){
				$this->error('身份证编号不能为空');
			}
			if(strlen($param['no'])!=15 and strlen($param['no'])!=18){
				$this->error('身份证号位数必须为15位或18位');
			}
	}
	public function files(){
			if(empty($_FILES['pic_f'])){
				$this->error('身份证正面不能为空');
			}
			if(empty($_FILES['pic_b'])){
				$this->error('身份证背面不能为空');
			}
	}
	public function edit(){
		$m=M('Idcard', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$id=I('id');
		$info=$m->find($id);
		// dump(I());
		$province_list=province_list();
		$this->assign('province_list',$province_list);
		$city_list=city_list($info['province']);
		$this->assign('city_list',$city_list);
		$area_list=area_list($info['province'],$info['city']);
		$this->assign('area_list',$area_list);
		$this->assign('detail',$info);
		$this->display();
	}
	//编辑
	public function confirm_edit(){
			$m=M('Idcard', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			if (!empty($_FILES)){
				$config = array(
					'maxSize'    =>    3145728,    
					'savePath'   =>    '/Import/',    
					'saveName'   =>    array('uniqid',''),    
					'exts'       =>    array('jpg','jpeg','png','gif'),    
					'autoSub'    =>    true,    
					'subName'    =>    array('date','Ymd'),);
				$upload = new \Think\Upload($config);// 实例化上传类
				$info   =   $upload->upload();    
				if(!$info) {// 上传错误提示错误信息        
					$this->error($upload->getError());    
				}
				if(!empty($info['pic_f'])){
					$extensionf=$info['pic_f']['ext'];
					$file_namef='./Uploads'.$info['pic_f']['savepath'].$info['pic_f']['savename'];
				
					textgd($file_namef);//加水印
					$param['pic_f']=$file_namef;
				}
				
				if(!empty($info['pic_b'])){
					$extensionb=$info['pic_b']['ext'];
					$file_nameb='./Uploads'.$info['pic_b']['savepath'].$info['pic_b']['savename'];
					
					textgd($file_nameb);//加水印
					$param['pic_b']=$file_nameb;
				}
			}
			$this->verification($param);
			$map['id']=$param['id'];
			$dup=$m->where(array('no'=>$param['no'],'id'=>array('neq',$param['id'])))->find();
			if($dup){
				$this->error('身份证号已经存在');
			}
			$pic_f=$_FILES['pic_f'];
			$pic_b=$_FILES['pic_b'];
			if(empty($pic_f)){
				unset($param['pic_f']);
			}
			if(empty($pic_b)){
				unset($param['pic_b']);
			}
			$res=$m->where($map)->save($param);
			if($res!==false){
				$this->success('编辑成功');
			}
	}
	//删除
	public function del(){
		$id=trim(I('id'));
		$m=M('Idcard', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		$status=$m->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	//批量删除
	public function delAll(){
		$ids=I('ids');
		$map['id']=array('in',$ids);
		$m=M('Idcard', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$status=$m->where($map)->delete();
		if($status){
			$this->success('删除成功');
		}else{
			$this->error('出错');
		}
	}

}